
#include "hw_config.h"
#include "led.h"
#include "uart.h"
#include "mmc_sd.h"
#include "diskio.h"
#include "ff.h"

static FATFS fs;            // Work area (file system object) for logical drive
static char path[20] = {""}; 	
static DIR dirs;
static FILINFO finfo;

void sd_file_list(void)
{
	u32 sd_size;
	char tx_data[16]; 

	usart0_puts("\r\nsd list start.\r\n");


	if( disk_initialize(0) == 0 )
		usart0_puts("\r\nsd initialize success.\r\n");
	else
		usart0_puts("\r\nsd initialize failed.\r\n");
  
#if 1


	f_mount(0,&fs);

	usart0_puts("\r\nf_mount()\r\n");	

	if (f_opendir (&dirs, path) == FR_OK) 
  	{
  		usart0_puts("\r\nf_opendir()\r\n");	
      	while (f_readdir (&dirs, &finfo) == FR_OK)  
    	{
    		usart0_puts("\r\nf_readdir()\r\n");	
			// usart0_format_puts ("\r\n%s", finfo.fname);
      		if (!(finfo.fattrib & AM_DIR)) 
      		{
      			
    			if (!finfo.fname[0])	
    			{
    				// usart0_puts("\r\nfinfo.fname\r\n");	
      				break;         

				}
        		usart0_format_puts ("\r\n%s:Folder", finfo.fname);
      		}                      
			else
				usart0_format_puts ("\r\n%s:File", finfo.fname);
      	}



	}
#endif
	sd_size = SD_GetCapacity();

#ifdef USE_AVR_STUDIO
	ultoa( sd_size, tx_data, 10);
#else
	ultoa( tx_data, sd_size, 10);
#endif
	usart0_format_puts("\r\nCapacity=%s Bytes\r\n", tx_data);
}

void main(void)
{

	CLI();	// all interrupt disable

	bsp_led_gpio_init();

#if 1
	bsp_usart0_gpio_init();
	

	if( F_CPU == 8000000UL ) 	
	{
		bsp_usart0_init(F_CPU, EBaud38400, EData8, EParNone, EStop1, FALSE );
	}
	else
	{
		bsp_usart0_init(F_CPU, EBaud115200, EData8, EParNone, EStop1, FALSE );	
	}

	init_usart0_buffer();

	bsp_usart0_interrupt_enable();	

	SEI();	// all interrupt enable

	bsp_led_core_on(ledUserCore);

	sd_file_list();
#endif

	//bsp_led_core_on(ledUserCore);

	while( 1 )
	{
		_delay_ms(10);
	}

	return;
}
